package com.usian;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCrypt;

import java.util.ArrayList;
import java.util.List;

public class UserDetailServiceImpl implements UserDetailsService {


    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        List<GrantedAuthority> authorityList = new ArrayList<GrantedAuthority>();
        authorityList.add(new SimpleGrantedAuthority("ROLE_USER"));

        return new User(userName, "", authorityList);
    }

    public static void main(String[] args) {
        //生成 随机盐值
        String gensalt = BCrypt.gensalt();
        System.out.println(gensalt);
        //密码加密
        String pwd = BCrypt.hashpw("123456",gensalt);
        System.out.println(pwd);
        //校验密码是否正确
        boolean checkpw = BCrypt.checkpw("123456",
                "$2a$10$.UI5NzfAEpjGwzFOVM.1HOZU5Yu9uyABxP5/XkqKa6vEkNuzj2wF.");
        System.out.println(checkpw);
    }
}
